;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.LibraryAge", [])
        .controller("LibraryAgeCtrl", ["$scope", "$state", "$stateParams","ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state,$stateParams, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            var btnPermissionCode = {
                libraryAgeExport: window.ActionCode.libraryAgeExport
            };

            $scope.queryTypeList = [
                {code: 1, name: '日'},
                {code: 2, name: '月'},
                {code: 3, name: '年'}
            ];
            $scope.btnPermission = {
                libraryAgeExport: true
            };
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/report/exportLibraryAge")
            };
            $scope.condition = {queryType: 1};
            $scope.dataList = [];
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.tbConf = {
                scrollWidth: 1200,
                tableCol: [
                    {
                        name: "条码",
                        dataField: "barCode",
                        width: 160,
                        isShow: true
                    },
                    {
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 160,
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsName",
                        width: 160,
                        isShow: true
                    },
                    {
                        name: "产品组",
                        dataField: "productGroup",
                        width: 160,
                        isShow: true
                    },
                    {
                        name: "零件类别",
                        dataField: "materialsType",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "仓库",
                        dataField: "warehouseCode",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "库区",
                        dataField: "areaCode",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "储位",
                        dataField: "locationCode",
                        width: 160,
                        isShow: true
                    },
                    {
                        name: "数量",
                        dataField: "amount",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "积压天数",
                        dataField: "days",
                        width: 100,
                        isShow: true
                    }
                ]
            };

            $scope.reset = function () {
                $scope.condition = {queryType: 1};
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.switchToolbar = function (toolbar) {
                $stateParams.backlogData =false;
                $scope.condition.minValue = undefined;
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
            };

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                var service = "device";
                var reqUrl = "/report/libraryAge";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                if(!!$stateParams.backlogData){
                    param.filters.minValue = 90;
                }
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }
            function getStockAlarmList(){
                if(!!$stateParams.backlogData){
                    $scope.condition.minValue = 90;
                    getDataList();
                }
            }
            
            (function () {
                // getDataList();
                getStockAlarmList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);